package com.sfx.算法专题.双指针.同向双指针.快慢指针;

/**
 * Created with IntelliJ IDEA.
 * Description:https://leetcode.cn/problems/happy-number/description/
 * User: sfx
 * Date: 2023-07-18
 * Time: 12:21
 */
public class Code_1_isHappy {

    static class Solution {
        private int getsquare(int n) {
            int sum = 0;
            while(n!=0) {
                sum += ((n%10)*(n%10));
                n /= 10;
            }
            return sum;
        }
        public boolean isHappy(int n) {
            //得到数组中的每一个数,然后平方
            //也要么最终结果得到1,要么始终无限循环
            int slow = n;
            int fast = getsquare(n);
            //寻找相遇节点
            while(fast != slow) {
                //每次slow走一步.
                slow = getsquare(slow);
                //fast走两步.
                fast = getsquare(getsquare(fast));
            }
            //2.根据相遇节点判断是否是快乐数
            return slow == 1;
        }
    }
}